﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Benchmark.Db.Base.DataObjects;

namespace Benchmark.Db.Base
{
    public class LengthAttribute : Attribute
    {
        public int Lenght { get; set; }

        public LengthAttribute(int lenght)
        {
            this.Lenght = lenght;
        }
    }

    public class PrimaryKeyAttribute : Attribute { }

    public class HasIndexAttribute : Attribute { }

    public class UseForLoadTestAttribute : Attribute { }

    public class MaxValueAttribute : Attribute
    {
        public int maxValue { get; set; }

        public MaxValueAttribute(int maxValue)
        {
            this.maxValue = maxValue;
        }
    }

    public static class AttributeHelper<A, T>
        where A : Attribute
        where T : class, IDataObject, new()
    {
        public static PropertyInfo[] GetDefinedProperties()
        {
            List<PropertyInfo> props = new List<PropertyInfo>();

            Type classType = typeof(T);
            PropertyInfo[] properties = classType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty);

            foreach (PropertyInfo prop in properties)
            {
                if (Attribute.IsDefined(prop, typeof(A), true))
                    props.Add(prop);
            }

            return props.ToArray();
        }

        public static A GetAttribute(PropertyInfo pInfo)
        {
            foreach (Attribute attribute in pInfo.GetCustomAttributes(true))
            {
                if (attribute.GetType() == typeof(A))
                    return attribute as A;
            }

            return default(A);
        }
    }

}
